<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader">

                <el-form-item label="类型" prop="type">
                    <ec-dict-select v-model="searchForm.enablestate" placeholder="选择类型" dictType="enablestate">
                    </ec-dict-select>
                </el-form-item>

                <el-form-item label="参数名" prop="name">
                    <el-input v-model="searchForm.name" placeholder="请输入名称" clearable />
                </el-form-item>

                <el-form-item label="操作">
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()"
                    v-if="isAuth()">添加
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteHandle()"
                    :disabled="!multipleSelection.length >0" v-if="isAuth()">批量删除
                    <span v-if="multipleSelection.length >0">【{{multipleSelection.length}}条】</span>
                </el-button>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share" v-if="isAuth()"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tabledata" @selection-change="handleSelectionChange">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="pkModule" header-align="center" align="center" show-overflow-tooltip label="主键">
            </el-table-column>
            <el-table-column prop="moduleName" header-align="center" align="center" show-overflow-tooltip label="模块名">
            </el-table-column>
            <el-table-column prop="moduleCode" header-align="center" align="center" show-overflow-tooltip label="模块代码">
            </el-table-column>
            <el-table-column prop="moduleDesc" header-align="center" align="center" show-overflow-tooltip label="模块描述">
            </el-table-column>
            <el-table-column prop="databaseName" header-align="center" align="center" show-overflow-tooltip
                label="数据库名称">
            </el-table-column>
            <el-table-column prop="databaseAddress" header-align="center" align="center" show-overflow-tooltip
                label="数据库地址">
            </el-table-column>
            <el-table-column prop="databaseAccount" header-align="center" align="center" show-overflow-tooltip
                label="数据库账号">
            </el-table-column>
            <el-table-column prop="databasePassword" header-align="center" align="center" show-overflow-tooltip
                label="数据库密码">
            </el-table-column>
            <el-table-column prop="createdByName" header-align="center" align="center" show-overflow-tooltip label="创建人">
            </el-table-column>
            <el-table-column prop="createdTime" header-align="center" align="center" show-overflow-tooltip label="创建时间">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="120" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.pkModule)" class="el-icon-edit"
                        v-if="isAuth()">修改
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.pkModule)" class="el-icon-delete"
                        v-if="isAuth()">删除
                    </el-button>
                </template>
            </el-table-column>
        </ec-table>

        <!-- 表格表尾 -->
        <template slot="footer">
            <!-- <el-button type="primary" size="mini" :loading="uploading" style="float:left;">
                <d2-icon name="cloud-upload" />
                Upload log data
            </el-button> -->

            <ec-pagination :page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </ec-pagination>
        </template>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>
    </d2-container>
</template>

<script>
    import * as centerService from '@/api/development/center'
    import AddOrUpdate from './add-or-update'

    export default {
        name: 'cache',
        data() {
            return {
                searchForm: {
                    type: '',
                    name: '',
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                showHeader: false,
                tabledata: [],
                multipleSelection: [],
                loading: false,
                addOrUpdateVisible: false
            }
        },
        components: {
            AddOrUpdate
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    type: this.searchForm.type,
                    name: this.searchForm.name
                };
                centerService.getCenterPage(query).then(data => {
                    this.tabledata = data.list;
                    this.page.total = data.totalCount;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            //切换页数
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            // 翻页
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 多选事件
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 删除/批量删除
            deleteHandle(id) {
                var ids = id ? [id] : this.multipleSelection.map(item => {
                    return item.pkModule
                })
                if (ids.length > 0) {
                    this.$confirm("确认删除[" + ids.length + "]条数据？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        centerService.delCenter(id).then((data) => {
                            this.$message.success("删除成功！");
                            this.getDataTable();
                        });
                    });
                } else {
                    this.$message.error("请选择数据！")
                }
            }
        }
    }
</script>